package logic

import (
	"context"
	"errors"
	"person/model/mysql"

	"person/userrpc/internal/svc"
	"person/userrpc/userrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserUpdateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUserUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserUpdateLogic {
	return &UserUpdateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 完善
func (l *UserUpdateLogic) UserUpdate(in *userrpc.UserUpdateRequest) (*userrpc.UserUpdateResponse, error) {
	// todo: add your logic here and delete this line

	var user mysql.User

	err := user.FindUserById(mysql.DB, in.UserId)
	if err != nil {
		return nil, errors.New("当前用户不存在")
	}

	in.Mobile = user.Mobile
	in.Password = user.Password

	if err := user.UserUpdate(mysql.DB); err != nil {
		return nil, errors.New("完善信息失败")
	}

	return &userrpc.UserUpdateResponse{
		UserId: int64(user.ID),
	}, nil
}
